package com.pap.datas.dict.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.datas.agent.dict.IDictAgent;
import com.pap.datas.agent.dict.dto.DictDTO;
import com.pap.datas.agent.dict.vo.DictVO;
import com.pap.datas.annotation.LoginedUser;
import com.pap.datas.dict.auto.entity.Dict;
import com.pap.obj.vo.logineduser.LoginedUserVO;
import com.pap.obj.vo.response.ResponseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/v1/data/dict")
@Api(value="字典操作接口",tags={"字典操作接口"})
public class DictController extends BaseController<Dict, IDictAgent> {

	@Resource(name = "dictAgent")
	@Override
	public void setService(IDictAgent service) {
		this.service = service;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveorupdate", method = {RequestMethod.POST})
	@ApiOperation(value="保存/更新字典信息", notes="保存/更新字典信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "clientLicenseId", value = "授权码标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "loginUserId", value = "登录用户标识", required = false, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = "saveJSON", value = "字典信息:DictDTO.class" , required = true, paramType = "query", dataType = "String", dataTypeClass = DictDTO.class)
	})
	public ResponseVO<Dict> saveOrUpdate(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "saveJSON", required = true) String saveJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		DictDTO saveDictObj = objectMapper.readValue(saveJSON, DictDTO.class);
		int operInt = service.saveOrUpdateDictWithDetailInfo(saveDictObj, clientLicenseId, loginUserId);
		
		return ResponseVO.successdata(operInt);
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/delete")
	public ResponseVO<Dict> delete(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "deleteJSON", required = true) String deleteJSON) throws Exception {
		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		DictDTO deleteDictObj = objectMapper.readValue(deleteJSON, DictDTO.class);
		
		int operInt = service.deleteDictWithDetailInfo(deleteDictObj, clientLicenseId, loginUserId);
		
		return ResponseVO.successdata(operInt);
	}

	@RequestMapping(value = "/deletebyid/{id}", method = {RequestMethod.DELETE})
	public ResponseVO<Dict> deleteById(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
								   @RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
									   @PathVariable(value = "id") String id) throws Exception {
		DictDTO deleteDictObj = new DictDTO();
		deleteDictObj.setId(id);

		int operInt = service.deleteDictWithDetailInfo(deleteDictObj, clientLicenseId, loginUserId);

		return ResponseVO.successdata(operInt);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/selectinfobyId/{id}", method = {RequestMethod.GET})
	public ResponseVO<DictDTO> selectInfoById(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String id) throws Exception {
		
		DictDTO operDictDTO = service.selectDictWithDetailInfo(id, clientLicenseId, loginUserId);
		
		return ResponseVO.successdata(operDictDTO);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/selectinfobycode/{code}", method = {RequestMethod.GET})
	public ResponseVO<DictDTO> selectinfobyCode(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String code) throws Exception {
		
		DictDTO operDictDTO = service.selectDictWithDetailInfoByCode(code, clientLicenseId, loginUserId);
		
		return ResponseVO.successdata(operDictDTO);
	}

	/**
	 * 参数校验，增加自定义参数校验部分(注解)
	 *	入参校验测试处理.
	 * @param vo 前端返回过来字典信息
	 * @return 成功的信息or失败信息(含参数校验失败部分)
	 */
	@RequestMapping(path = "/valid")
	public ResponseVO valid(@Valid @RequestBody DictVO vo) {
		return ResponseVO.successdata(vo);
	}


	/**
	 * 优雅的获取登录用户信息
	 * @param loginedUserVO	当前登录用户的VO类
	 * @return
	 */
	@RequestMapping(path = "/logineduser")
	public ResponseVO loginedUser(@LoginedUser LoginedUserVO loginedUserVO) {
		return ResponseVO.successdata(loginedUserVO);
	}

}
